package impl;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class Client {
	private DataInputStream input;
	private DataOutputStream output;
	private boolean connect;
	private static String INPUT_PROMPT = "Input   >";
	private Scanner userInput = new Scanner(System.in);

	public Client(String host, int port) {
		try {
			Socket socket = new Socket(host, port);

			System.out.println("Connected to socket " + socket);

			input = new DataInputStream(socket.getInputStream());
			output = new DataOutputStream(socket.getOutputStream());

			connect = true;

			ClientThread thread = new ClientThread(this, input);
			
			System.out.println("Type \"exit\" to disconnect the client.");
			while (connect) {
				System.out.print(INPUT_PROMPT);
				String message = userInput.next();
				message = message + userInput.nextLine();
				if (message.equalsIgnoreCase("exit")) {
					connect = false;
				} else {
					processMessage(message);
				}
			}

		} catch (IOException e) {
			System.out.println(e.getStackTrace());
		}
	}

	public void processMessage(String message) {
		try {
			// write the entered message to the output stream that will send the
			// message to the server
			output.writeUTF(message);
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}

	public boolean isConnect() {
		return connect;
	}

	public void setConnect(boolean connect) {
		this.connect = connect;
	}
}
